package com.example.xd.commen;

import android.content.Context;
import android.util.Log;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.annotation.Interceptor;
import com.alibaba.android.arouter.facade.callback.InterceptorCallback;
import com.alibaba.android.arouter.facade.template.IInterceptor;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * @auther XuDan on 2019/7/22.
 */
@Interceptor(priority = 1)
public class ShakeIntercepter implements IInterceptor {
    Context context;
    HashMap<String, Long> hm = new HashMap<>();

    @Override
    public void process(Postcard postcard, InterceptorCallback callback) {
        String path = postcard.getPath();
        if (hm.containsKey(path)) {
            Long time = hm.get(path);
            long l = System.currentTimeMillis();
            long l1 = (l - time) / 1000;
            Log.i("xd---------", l1 + "秒");
            if (l1 > 0.5) {
                callback.onContinue(postcard);
            } else {
                callback.onInterrupt(new Throwable("时间太短"));
            }
            hm.put(path, l);
        } else {
            long l = System.currentTimeMillis();
            hm.put(path, l);
            callback.onContinue(postcard);
        }
    }

    @Override
    public void init(Context context) {
        this.context = context;
    }
}
